package com.bit.cfdi.web.vm.negocio;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.GlobalCommand;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.select.annotation.VariableResolver;
import org.zkoss.zk.ui.select.annotation.WireVariable;
import org.zkoss.zul.Window;

import com.bit.cfdi.web.domain.Negocio;
import com.bit.cfdi.web.domain.NegocioGiro;
import com.bit.cfdi.web.services.GiroService;
import com.bit.cfdi.web.services.NegocioGiroService;
import com.bit.cfdi.web.session.SessionUtils;

@VariableResolver(org.zkoss.zkplus.spring.DelegatingVariableResolver.class)
public class ConfigurarGiroNegocioViewModel {

	private Logger log = LogManager.getLogger(this.getClass().getName());
	private String nameGiros;
	private Window window;
	private Negocio negocio;
	private NegocioGiro negocioGiro;
	private List<WrapperGiros> listGiros;
	private List<NegocioGiro> listNegocioGiro;
	private boolean visibleBtnSave;
	@WireVariable
	private SessionUtils sessionUtils;
	@WireVariable
	private NegocioGiroService negocioGiroService;
	@WireVariable
	private GiroService giroService;

	@Init
	public void init() {
		negocio = (Negocio) sessionUtils.getFromSession(SessionUtils.NEGOCIO);
		listGiros = new ArrayList<WrapperGiros>();
		listNegocioGiro = negocioGiroService.getFromNegocio(negocio);
		if (listNegocioGiro != null) {
			loadSelectedGiros();
		}
		visibleBtnSave = false;
	}

	public String getNameGiros() {
		return nameGiros;
	}

	public void setNameGiros(String nameGiros) {
		this.nameGiros = nameGiros;
	}

	public boolean getVisibleBtnSave() {
		return visibleBtnSave;
	}

	@Command
	public void showPopUpGiros() {
		Map<String, Object> objList = new HashMap<String, Object>();
		objList.put("listGiros", listGiros);

		window = (Window) Executions.createComponents(
				"/home/negocio/modalWindowGiros1.zul", null, objList);
		window.doModal();
	}

	@Command
	public void saveGiros() {
		if (listGiros != null) {
			negocioGiroService.removeAll(negocio);
			for (WrapperGiros currentGiro : listGiros) {
				try {
					negocioGiro = new NegocioGiro();
					negocioGiro.setNegocio(negocio);
					negocioGiro.setGiro(currentGiro.getGiro());
					negocioGiro.setIsDefault(currentGiro.getStatus());
					negocioGiroService.save(negocioGiro);
				} catch (DataAccessException e) {
					log.warn("Ha ocurrido un error grave al intentar persistit"
							+ "la información del negocio", e);
				}
			}
		}
	}

	@GlobalCommand
	@NotifyChange({ "nameGiros", "visibleBtnSave" })
	public void processGiros(
			@BindingParam("listGiros") List<WrapperGiros> listGirosSelected) {
		visibleBtnSave = true;
		nameGiros = "";
		boolean primerItem = true;
		listGiros = listGirosSelected;
		for (WrapperGiros current : listGirosSelected) {
			if (primerItem) {
				nameGiros = current.getNombreGiro();
			} else {
				nameGiros = nameGiros + " , " + current.getNombreGiro();
			}
			primerItem = false;
		}
	}

	// -----
	@GlobalCommand
	public void wizardHideGiroPopUp() {
		if (window != null) {
			window.setVisible(false);
			window = null;
		}
	}

	@NotifyChange({ "nameGiros" })
	public void loadSelectedGiros() {
		boolean primerItem = true;
		for (NegocioGiro currentNG : listNegocioGiro) {
			WrapperGiros objWrapper = new WrapperGiros();
			objWrapper.setGiro(currentNG.getGiro());
			objWrapper.setNombreGiro(currentNG.getGiro().getDescripcion());
			objWrapper.setStatus(currentNG.getIsDefault());
			listGiros.add(objWrapper);
			if (primerItem) {
				nameGiros = currentNG.getGiro().getDescripcion();
			} else {
				nameGiros = nameGiros + " , "
						+ currentNG.getGiro().getDescripcion();
			}
			primerItem = false;
		}
	}
}